package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CBAServlet
 */
@WebServlet("/CBAServlet")
public class CBAServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CBAServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		String action = request.getParameter("action");
		if(action == null || "".equals(action)) {
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter()
			.append("本页面是程序首页，为浏览器用户提供了若干功能<br/>")
            .append("在URL后添加<font color='red'查询字符串></font>，测试response各功能<br/>")
            .append("？action=statusCode 测试响应状态码，在Tomcat控制台查看本程序的响应状态码<br/>")
            .append("？action=header 注意使用F12，打开浏览器使用开发者工具<br/>")
            .append("？action=refresh 查看Tomcat控制台，refresh后的代码是否被执行<br/>")
            .append("？action=redirect 查看Tomcat控制台，redirect后的代码是否被执行<br/>")
            .append("？action=forward 查看Tomcat控制台，请求转发后的代码是否被执行<br/>")
            .append("？action=include 查看Tomcat控制台，请求包含后的代码是否被执行<br/>")
			.append("？action=contentType 不指定浏览器解码方式，乱码了吗？<br/>")
            .append("？action=chars 文本型数据里不能掺杂字节数据，否则异常<br/>")
            .append("？action=bytes 字节型数据里不能掺杂文本数据，否则异常<br/>")
            .append("？action=picture&fileName=学习强国.jpg 通过response预览图片<br/>")
            .append("？action=picture&subAction=download&fileName=学习强国.jpg 通过response下载图片文件<br/>")
            .append("？action=img&fileName=学习强国.jpg 使用img标签预览图片<br/>")
            .append("？action=imgServlet&fileName=学习强国.jpg 使用img标签和response的结合预览图片<br/>")
            
            ;
			return;
			
		}
		System.out.println("开始测试"+action+"功能");
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print("该功能尚未开发，期待您的开发升级!<br/>");
		if(action.equals("statusCode")) {
			System.out.println("设置前的状态码是："+response.getStatus());
			response.sendError(404,"您访问的页面跑到火星上去了！");
			System.out.println("设置后的状态码是："+response.getStatus());
			return;	
		}
		
		if(action.equals("header")) {
			response.setHeader("Content-Type", "text/html;charset=UTF-8");
			response.setIntHeader("Content-Length", 0);
			response.setDateHeader("Date", System.currentTimeMillis());
			System.out.println("包含Set-Cookie响应头吗？"+response.containsHeader("Set-Cookie"));
			response.addHeader("Set-Cookie", "username=zhangsan;Path=/;HttpOnly");
			response.addHeader("Set-Cookie", "password=112345;Path=/;HttpOnly");
			System.out.println("包含Set-Cookie响应头吗？"+response.containsHeader("Set-Cookie"));
			System.out.println("Set-Cookie响应头是"+response.getHeader("Set-Cookie"));
			java.util.Collection<java.lang.String>headerNames = response.getHeaderNames();
			for(String headerName :headerNames) {
				System.out.print(headerName + "是: ");
				java.util.Collection<java.lang.String>headerValues = response.getHeaders(headerName);
				for(String headerValue:headerValues) {
					System.out.println(headerValue);
				}
			}
			return;	
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
